//
//  YTLeftView.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/12.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTLeftView.h"
#import "YTTimingUtilsView.h"
#import "YTDelayView.h"

@interface YTLeftView ()<YTLeftModelCcontrolViewDelegate>
{
    int aatest;
}
@property(nonatomic, strong)UIButton *switchBtn;
@property(nonatomic, strong)UIButton *photoBtn;
@property(nonatomic, strong)UIButton *videoBtn;
@property(nonatomic, strong)UIImageView *cicleImageView;

@property(nonatomic, strong)UIButton *modelBtn;
@property(nonatomic, strong)UIButton *shutterBtn;
@property(nonatomic, strong)UIButton *switchCameraBtn;
@property(nonatomic, strong)UIButton *albumBtn;
@property(nonatomic, weak)UIViewController *previewVC;

@property(nonatomic, strong)dispatch_source_t timingTimer;
@property (nonatomic, strong) UILabel *exposoureTimeLabel;

@property(nonatomic, strong)YTLeftModelCcontrolView *leftModelCcontrolView;
@property(nonatomic, strong)YTDelayView *delayView;
@end

@implementation YTLeftView
#pragma mark life cycle method
-(instancetype)initWithVC:(UIViewController *)previewVC
{
    self = [super init];
    if (self)
    {
        //        self.frame = CGRectMake(0, 0, 100, ScreenHeight);
        self.previewVC = previewVC;
        self.userInteractionEnabled = YES;
        [self p_addSubViews];
        [self p_makeConstraints];
        [self p_configButtonStatus];
    }
    return self;
}
-(instancetype)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self p_makeConstraints];
    [YTTimingUtilsView p_layoutSubviews];
}
#pragma mark private method
-(void)p_addSubViews
{
    [self addSubview:self.switchBtn];
    [_switchBtn addSubview:self.cicleImageView];
    [_switchBtn addSubview:self.photoBtn];
    [_switchBtn addSubview:self.videoBtn];
    
    [self addSubview:self.modelBtn];
    [self addSubview:self.shutterBtn];
    [self addSubview:self.switchCameraBtn];
    [self addSubview:self.albumBtn];
    [self addSubview:self.exposoureTimeLabel];
}
-(void)p_makeConstraints
{
    if([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait||[UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown)
    {
        //竖屏
        CGFloat width = ScreenWidth/5;
        [_shutterBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.height.mas_equalTo(124/2);
            make.width.mas_equalTo(width);
        }];
        [_modelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(_shutterBtn.mas_leading);
            make.centerY.equalTo(_shutterBtn);
            make.width.height.equalTo(_shutterBtn);
        }];
        [_switchBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(width/2 - 124/2/2);
            make.centerY.width.equalTo(_shutterBtn);
            make.height.mas_equalTo(124/2);
        }];
        [_cicleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_switchBtn);
            if (publicModel.isPhoto)
            {
                make.centerY.equalTo(_switchBtn).offset(-124/2/2/2);
            }else
            {
                make.centerY.equalTo(_switchBtn).offset(124/2/2/2);
            }
            make.width.height.mas_equalTo(58/2);
        }];
        [_photoBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_switchBtn);
            make.centerY.equalTo(_switchBtn).offset(-124/2/2/2);
            make.width.mas_equalTo(30/2);
            make.height.mas_equalTo(24/2);
        }];
        [_videoBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_switchBtn);
            make.centerY.equalTo(_switchBtn).offset(124/2/2/2);
            make.width.height.mas_equalTo(30/2);
        }];
        [_switchCameraBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.width.height.equalTo(_shutterBtn);
            make.leading.equalTo(_shutterBtn.mas_trailing);
        }];
        [_albumBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.width.height.equalTo(_shutterBtn);
            make.leading.equalTo(_switchCameraBtn.mas_trailing);
        }];
    }else
    {
        //横屏
//        self.frame = CGRectMake(0, 0, 100, ScreenHeight);
        CGFloat height = ScreenHeight/5;
        [_shutterBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.mas_equalTo(120/2);
            make.height.mas_equalTo(height);
        }];
        [_modelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_shutterBtn.mas_top);
            make.centerX.equalTo(_shutterBtn);
            make.width.height.equalTo(_shutterBtn);
        }];
        [_switchBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(height/2-120/2/2);
            make.centerX.height.equalTo(_shutterBtn);
            make.width.mas_equalTo(70/2);
        }];
        [_cicleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_switchBtn);
            if (publicModel.isPhoto)
            {
                make.centerY.equalTo(_switchBtn).offset(-124/2/2/2);
            }else
            {
                make.centerY.equalTo(_switchBtn).offset(124/2/2/2);
            }
            make.width.height.mas_equalTo(58/2);
        }];
        [_photoBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_switchBtn);
            make.centerY.equalTo(_switchBtn).offset(-124/2/2/2);
            make.width.mas_equalTo(30/2);
            make.height.mas_equalTo(24/2);
        }];
        [_videoBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_switchBtn);
            make.centerY.equalTo(_switchBtn).offset(124/2/2/2);
            make.width.height.mas_equalTo(30/2);
        }];
        [_switchCameraBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.width.height.equalTo(_shutterBtn);
            make.top.equalTo(_shutterBtn.mas_bottom);
        }];
        [_albumBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.width.height.equalTo(_shutterBtn);
            make.top.equalTo(_switchCameraBtn.mas_bottom);
        }];
    }
    if (_leftModelCcontrolView)
    {
        [_leftModelCcontrolView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.leading.trailing.equalTo(_previewVC.view);
        }];
    }
    if (_delayView)
    {
        [_delayView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(_previewVC.view);
        }];
    }
    [self.exposoureTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.shutterBtn.center_Y).offset(-self.shutterBtn.height * 0.5 - 15);
        make.centerX.mas_equalTo(self.shutterBtn.center_X);
        make.width.equalTo(@(100));
        make.height.equalTo(@(20));
    }];
   
}
#pragma mark btn action
/*
-(void)btnClickAction:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    if (tag == 601)
    {//切换拍照录像
        _switchBtn.selected = !_switchBtn.isSelected;
        publicModel.isPhoto = _switchBtn.selected;
        [self p_configButtonStatus];
    }else if (tag == 602)
    {//模式设置
        _leftModelCcontrolView = [[YTLeftModelCcontrolView alloc] initWithBlock:^{
            [_leftModelCcontrolView removeFromSuperview];
            _leftModelCcontrolView = nil;
        }];
        _leftModelCcontrolView.userInteractionEnabled = YES;
        _leftModelCcontrolView.delegate = self;
        [_previewVC.view addSubview:_leftModelCcontrolView];
        [self p_makeConstraints];
    }else if (tag == 603)
    {//快门按钮
        NSLog(@"%i", publicModel.isPhoto);
        if (!publicModel.isPhoto)
        {
            sender.selected = !sender.isSelected;
            if (sender.isSelected)
            {[deviceManager record];}else
            {[deviceManager pause];}
        }else{
            [self p_takePhoto];
            
        }
    }else if (tag == 604)
    {//切换前后摄像头
        [deviceManager switchCamera];
    }else if (tag == 605)
    {//相册
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateAlbumAction:)])
        {
            [self.delegate delegateAlbumAction:sender];
        }
    }

}
 */
/*
-(void)p_takePhoto
{
    if (publicModel.photoModel == 0)
    {
        int time = 0;
        switch (publicModel.modelSingle)
        {
            case 0:{time = 0;}
                break;
            case 1:{time = 2;}
                break;
            case 2:{time = 5;}
                break;
            case 3:{time = 10;}
                break;
            case 4:{time = 0;}
                break;
            default:
                break;
        }
        [YTTimingUtilsView showTimingPhoto:time];
        self.timingTimer = [YTTools gcdTimer:1 DelayTime:time];
        dispatch_source_set_event_handler(self.timingTimer, ^{
            [YTTimingUtilsView dismissUtilsView];
            [deviceManager takePhoto];
            if (self.timingTimer)
            {
                dispatch_cancel(self.timingTimer);
                self.timingTimer = nil;
            }
        });
        dispatch_resume(self.timingTimer);
    }else if (publicModel.photoModel == 1)
    {
        
    }else if (publicModel.photoModel == 2)
    {
        
    }
}
 */

#pragma mark YTLeftModelCcontrolViewDelegate
-(void)delegateLeftModelAction:(NSIndexPath *)indexPath
{
    if (publicModel.isPhoto)
    {
        
    }else
    {
        if (indexPath.row == 2)
        {//延时摄影
            NSLog(@"");
            _delayView = [[YTDelayView alloc] initWithBlock:^{
                [_delayView removeFromSuperview];
                _delayView = nil;
            }];
            [_previewVC.view addSubview:_delayView];
            [self p_makeConstraints];
        }
    }
}
#pragma mark lazy get method
-(UIButton *)switchBtn
{
    if (!_switchBtn)
    {
        _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_switchBtn setImage:IMAGE(@"97f") forState:UIControlStateNormal];
        _switchBtn.tag = 601;
        [_switchBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
        [_switchBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}
-(UIButton *)photoBtn
{
    if (!_photoBtn)
    {
        _photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_photoBtn setImage:IMAGE(@"97a") forState:UIControlStateNormal];
        [_photoBtn setImage:IMAGE(@"97") forState:UIControlStateSelected];
        _photoBtn.tag = 601;
        [_photoBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
        [_photoBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _photoBtn;
}
-(UIButton *)videoBtn
{
    if (!_videoBtn)
    {
        _videoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_videoBtn setImage:IMAGE(@"97b") forState:UIControlStateNormal];
        [_videoBtn setImage:IMAGE(@"97c") forState:UIControlStateSelected];
        _videoBtn.tag = 601;
        [_videoBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
        [_videoBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _videoBtn;
}
-(UIImageView *)cicleImageView
{
    if (!_cicleImageView)
    {
        _cicleImageView = [[UIImageView alloc] init];
        _cicleImageView.image = IMAGE(@"97e");
        _cicleImageView.userInteractionEnabled = YES;
    }
    return _cicleImageView;
}
-(UIButton *)modelBtn
{
    if (!_modelBtn)
    {
        _modelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_modelBtn setImage:IMAGE(@"02") forState:UIControlStateNormal];
        _modelBtn.tag = 602;
        [_modelBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _modelBtn;
}
-(UIButton *)shutterBtn
{
    if (!_shutterBtn)
    {
        _shutterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shutterBtn setImage:IMAGE(@"03") forState:UIControlStateNormal];
        _shutterBtn.tag = 603;
        [_shutterBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shutterBtn;
}
-(UIButton *)switchCameraBtn
{
    if (!_switchCameraBtn)
    {
        _switchCameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_switchCameraBtn setImage:IMAGE(@"04") forState:UIControlStateNormal];
        _switchCameraBtn.tag = 604;
        [_switchCameraBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchCameraBtn;
}
-(UIButton *)albumBtn
{
    if (!_albumBtn)
    {
        _albumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_albumBtn setImage:IMAGE(@"05") forState:UIControlStateNormal];
        _albumBtn.tag = 605;
        [_albumBtn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _albumBtn;
}
- (UILabel *)exposoureTimeLabel
{
    if (!_exposoureTimeLabel) {
        _exposoureTimeLabel = [[UILabel alloc] init];
        _exposoureTimeLabel.font = [UIFont systemFontOfSize:12];
        _exposoureTimeLabel.textColor = [UIColor whiteColor];
        _exposoureTimeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _exposoureTimeLabel;
}
-(void)p_configButtonStatus
{
    if (!publicModel.isPhoto)
    {
        //        _cicleImageView.center = CGPointMake(_cicleImageView.center.x, 124/2/2+124/2/2/2);
        [UIView animateWithDuration:1 animations:^{
            [_cicleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(_switchBtn).offset(124/2/2/2);
            }];
        }];
        _shutterBtn.selected = NO;
        [_shutterBtn setImage:IMAGE(@"98a") forState:UIControlStateNormal];
        [_shutterBtn setImage:IMAGE(@"98") forState:UIControlStateSelected];
    }else
    {
        [UIView animateWithDuration:1 animations:^{
            [_cicleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(_switchBtn).offset(-124/2/2/2);
            }];
        }];
        //        _cicleImageView.center = CGPointMake(_cicleImageView.center.x, 124/2/2/2);
        _shutterBtn.selected = NO;
        [_shutterBtn setImage:IMAGE(@"03") forState:UIControlStateNormal];
        [_shutterBtn setImage:IMAGE(@"03a") forState:UIControlStateSelected];
    }
}
#pragma mark btn action
-(void)btnClickAction:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    if (tag == 601)
    {//切换拍照录像
        _switchBtn.selected = !_switchBtn.isSelected;
        publicModel.isPhoto = _switchBtn.selected;
        [self p_configButtonStatus];
    }else if (tag == 602)
    {//模式设置
        /*
        sender.selected = !sender.isSelected;
        if (sender.isSelected)
        {
            _leftModelCcontrolView = [[YTLeftModelCcontrolView alloc] init];
            [_previewVC.view addSubview:_leftModelCcontrolView];
            [self p_makeConstraints];
        }else
        {
            [_leftModelCcontrolView removeFromSuperview];
            _leftModelCcontrolView = nil;
        }
        */
        _leftModelCcontrolView = [[YTLeftModelCcontrolView alloc] initWithBlock:^{
            [_leftModelCcontrolView removeFromSuperview];
            _leftModelCcontrolView = nil;
        }];
        _leftModelCcontrolView.userInteractionEnabled = YES;
        _leftModelCcontrolView.delegate = self;
        [_previewVC.view addSubview:_leftModelCcontrolView];
        [self p_makeConstraints];
        

    }else if (tag == 603)
    {//快门按钮
        NSLog(@"%i", publicModel.isPhoto);
        if (!publicModel.isPhoto)
        {
            sender.selected = !sender.isSelected;
            if (sender.isSelected)
            {
                [deviceManager record];
            }else
            {
                [deviceManager pause];
            }
        }else
        {
            [self p_takePhoto];
        }
    }else if (tag == 604)
    {//切换前后摄像头
//        if (aatest == 0)
//        {
//            [deviceManager setVideoResolutionFps:10];
//            aatest = 1;
//        }else
//        {
//            aatest = 0;
//            [deviceManager setVideoResolutionFps:120];
//        }
//        return;
        sender.selected = !sender.isSelected;
        [deviceManager switchCamera];
    }else if (tag == 605)
    {//相册
//        [deviceManager configureCameraForHighestFrameRate];
//        return;
////        [deviceManager setExposure:aatest];
//        [deviceManager setVideoResolution:aatest];
//        aatest++;
//        return;
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateAlbumAction:)])
        {
            [self.delegate delegateAlbumAction:sender];
        }
    }
}
-(void)p_takePhoto
{

    if (publicModel.photoModel == 0)
    {
        int time = 0;
        switch (publicModel.modelSingle)
        {
            case 0:
            {
                time = 0;
            }
                break;
            case 1:
            {
                time = 2;
            }
                break;
            case 2:
            {
                time = 5;
            }
                break;
            case 3:
            {
                time = 10;
            }
                break;
            case 4:
            {
                time = 0;
            }
                break;
            default:
                break;
        }
        [YTTimingUtilsView showTimingPhoto:time];
        self.timingTimer = [YTTools gcdTimer:1 DelayTime:time];
        dispatch_source_set_event_handler(self.timingTimer, ^{
            [YTTimingUtilsView dismissUtilsView];
            [deviceManager takePhoto];
            if (self.timingTimer)
            {
                dispatch_cancel(self.timingTimer);
                self.timingTimer = nil;
            }
        });
        dispatch_resume(self.timingTimer);
    }else if (publicModel.photoModel == 1)
    {
        
    }else if (publicModel.photoModel == 2)
    {
        IMP_BLOCK_SELF(YTLeftView);
        if (publicModel.modelLong == 0) {
            publicModel.modelLong = 1;
            self.exposoureTimeLabel.hidden = NO;
            block_self.exposoureTimeLabel.text = @"00:00:00";
            [deviceManager setMoreExposureWithCount:^(id sender, id sender1) {
                block_self.exposoureTimeLabel.text = [YTTools getHHMMSSFromSS:[sender integerValue]];
            }];
        }else{
            self.exposoureTimeLabel.hidden = YES;
            [deviceManager endMoreExposure];
            publicModel.modelLong = 0;

        }
    }
}
@end
/**
 1. 视频参数设置
 这里是很核心的配置参数，要配合StreamEye以及相机捕捉去看自己的录制和系统录制的区别，主要是qa，清晰度等。
 码率的观看可以用苹果自带的相机软件，然后用Alt+I快捷键弹出一个信息看码率。
 千万不要用airdrop传文件到电脑，会导致重新编码。用苹果应用程序里面的相机捕捉工具就可以很好看的去看了。
 以上几点都是无数实践的踩坑。大家一定要注意，相应参数的设置错误，会导致如果同样是4k的录制，那么你录制的达不到30fps，或者要比系统的录制大小占用空间大很多。下面我提供了一个参考设置，都是经过和系统对比还有实践出来的一些参数。如果有错误，请大家在评论下方告知我，或者加入qq群237305299,不甚感激。

 NSDictionary *videoCompressionProps;
 NSDictionary *videoSettings;
 switch (cameraModel.videoResolution) {
 case AVCaptureSessionPreset3840x2160:
 videoCompressionProps = @{
 AVVideoAverageBitRateKey:@(50*1024.0*1024),
 AVVideoH264EntropyModeKey:AVVideoH264EntropyModeCABAC,
 AVVideoMaxKeyFrameIntervalKey:@(30),
 AVVideoAllowFrameReorderingKey:@NO,
 AVVideoExpectedSourceFrameRateKey:@30,
 };
 break;
 case AVCaptureSessionPreset1920x1080:
 videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithDouble:18*1024.0*1024], AVVideoAverageBitRateKey,
 AVVideoH264EntropyModeCABAC,AVVideoH264EntropyModeKey,
 nil];
 break;
 case AVCaptureSessionPreset1280x720:
 videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithDouble:8*1024.0*1024], AVVideoAverageBitRateKey,
 AVVideoH264EntropyModeCABAC,AVVideoH264EntropyModeKey,
 nil ];
 break;
 default:
 break;
 }
 videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
 AVVideoCodecH264,AVVideoCodecKey,
 videoCompressionProps, AVVideoCompressionPropertiesKey,
 AVVideoScalingModeResizeAspectFill,AVVideoScalingModeKey,
 [NSNumber numberWithInteger:videoSize.width],AVVideoWidthKey,
 [NSNumber numberWithInteger:videoSize.height],AVVideoHeightKey,
 nil];
 
 self.writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
 
 if(cameraModel.devicePosition == DJIIPhone_DevicePositionFront){
 self.writerInput.transform = [OSMOMediaUtils getAngleTransformFromScreenOritationFront];
 }else{
 self.writerInput.transform = [OSMOMediaUtils getAngleTransformFromScreenOritationBack];
 }
 
 self.writerInput.expectsMediaDataInRealTime = YES;
 2. 录制方向
 给AVWritter设置一个初始化的Transform,从摄像头过来的CMSampleBuffer直接进入录制流，不要去做屏幕流程。因为不这样的化会出现一个问题。
 你点击录制视频，这过程中去旋转屏幕，当你支持四个方向的时候，旋转的时候就会出现闪烁。具体可以用苹果自己的相机试一下就一清二楚了。
 四. GPU美颜处理后用CVPixBuffer编码直播
 GPU美颜处理
 错误处理方法:

 CGSize outputSize = {720, 1280};
 GPUImageRawDataOutput *rawDataOutput = [[GPUImageRawDataOutput alloc] initWithImageSize:CGSizeMake(outputSize.width, outputSize.height) resultsInBGRAFormat:YES];
 [self.beautifyFilter addTarget:rawDataOutput];
 将rawDataoutput作为Target加入，然后获取pixbuffer给videotoolbox处理
 
 __weak GPUImageRawDataOutput *weakOutput = rawDataOutput;
 __weak typeof(self) weakSelf = self;
 
 [rawDataOutput setNewFrameAvailableBlock:^{
 __strong GPUImageRawDataOutput *strongOutput = weakOutput;
 [strongOutput lockFramebufferForReading];
 
 // 这里就可以获取到添加滤镜的数据了
 GLubyte *outputBytes = [strongOutput rawBytesForImage];
 NSInteger bytesPerRow = [strongOutput bytesPerRowInOutput];
 CVPixelBufferRef pixelBuffer = NULL;
 CVPixelBufferCreateWithBytes(kCFAllocatorDefault, outputSize.width, outputSize.height, kCVPixelFormatType_32BGRA, outputBytes, bytesPerRow, nil, nil, nil, &pixelBuffer);
 
 // 之后可以利用VideoToolBox进行硬编码再结合rtmp协议传输视频流了
 [weakSelf encodeWithCVPixelBufferRef:pixelBuffer];
 
 [strongOutput unlockFramebufferAfterReading];
 CFRelease(pixelBuffer);
 }];
 大家可以看一下为什么我不推荐这种方法，CVPixelBufferCreateWithBytes这里是很耗时的，要重新创建。
 正确处理方法:
 正确的方法建议大家看一下GPUFrameBuffer，里面有一个renderTarget，加一个类别去获取renderTarget（CVPixBuffer)，我们取出来的时候lock一下，用完unlock一下就好了。
 五. 慢动作
 慢动作设置,这里的技术原理实则以指定超过60fps去执行一个录制，比如240fps，然后将240fps按照30fps去写入数据填充时间段，所以就有了慢动作效果。下面的代码是设置最大速度。
 
 -(void)configureCameraForHighestFrameRate:(AVCaptureDevice*) device
 {
 AVCaptureDeviceFormat *bestFormat = nil;
 AVFrameRateRange *bestFrameRateRange = nil;
 for(AVCaptureDeviceFormat *format in [device formats] ) {
 for ( AVFrameRateRange *range in format.videoSupportedFrameRateRanges ) {
 if ( range.maxFrameRate > bestFrameRateRange.maxFrameRate ) {
 bestFormat = format;
 bestFrameRateRange = range;
 }
 }
 }
 
 if ( bestFormat ) {
 if ( [device lockForConfiguration:NULL] == YES ) {
 device.activeFormat = bestFormat;
 device.activeVideoMinFrameDuration = bestFrameRateRange.minFrameDuration;
 device.activeVideoMaxFrameDuration = bestFrameRateRange.minFrameDuration;
 [device unlockForConfiguration];
 }
 }
 }
 六. 延时摄影
 由于系统是每秒回调30次，那么现在我们自己定时器每秒取一张图，然后利用AVAssetWriterInputPixelBufferAdaptor和AVAssetWriterInput,还有AVAssetWriter去用图片组帧视频。
 七. 星轨拍摄
 利用GPUBlenderFilter的双输入通道即可，叠加加上。生成照片之前记得把滤镜useNextCapture.
 */

